{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# PMOD Grove PIR Motion Sensor\n",
    "\n",
    "This example shows how to use the [Grove PIR motion sensor](http://www.seeedstudio.com/depot/Grove-PIR-Motion-Sensor-p-802.html).\n",
    "\n",
    "This demo requires the Grove PIR motion sensor and PYNQ Grove Adapter.\n",
    "\n",
    "### 1. Download overlay\n",
    "Download the overlay. Instantiate Grove PIR object. \n",
    "\n",
    "In this example, the Grove PIR sensor is assumed to be connected to G1 on the PYNQ Grove Adapter. The Grove Adapter is connected to PMODA interface on board."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "from pynq.overlays.base import BaseOverlay\n",
    "from pynq.lib.pmod import Grove_PIR\n",
    "from pynq.lib.pmod import PMOD_GROVE_G1\n",
    "\n",
    "ol1 = BaseOverlay(\"base.bit\")\n",
    "pir = Grove_PIR(ol1.PMODA,PMOD_GROVE_G1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Detect motion\n",
    "We can now start the motion detection loop. \n",
    "\n",
    "When the cell below is executed, the LED will keep flashing until the PIR detects some movement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting detection...\n",
      "Detected a motion.\n",
      "Ending detection...\n"
     ]
    }
   ],
   "source": [
    "led = ol1.leds[0]\n",
    "led.on()\n",
    "\n",
    "if pir.read()==0:\n",
    "    print(\"Starting detection...\")\n",
    "    while True:\n",
    "        led.on()\n",
    "        sleep(0.1)\n",
    "        led.off()\n",
    "        sleep(0.1)\n",
    "        if pir.read()==1:\n",
    "            print(\"Detected a motion.\")\n",
    "            break\n",
    "print(\"Ending detection...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Clean-up\n",
    "Delete objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "del pir\n",
    "del led\n",
    "del ol1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
